package com.csu.pet.demo.controller;

import com.csu.pet.demo.domain.Account;
import com.csu.pet.demo.domain.Log;
import com.csu.pet.demo.domain.Vertify;
import com.csu.pet.demo.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {
    @Autowired
    AccountService accountService;

    @Autowired
    Account account;

    String validString;

    //请求访问登录页面
    @GetMapping("/loginPage")
    public String requestForLoginPage(){
        return "/account/Login";
    }

    //请求登录（点击登录按钮）
    @PostMapping("/login")
    public String requestForLogin(String username,String password,String valistr,Model model) {
        System.out.println(username + password);
//        Account
        account = accountService.getAccount(username, password);
        //查询数据库中是否有这个用户
        if (account != null) {
            System.out.println("登录信息合法" + account.getUsername() + account.getPassword());
            //判断验证码是否正确
            if(valistr.equals("")){
                //验证码为空
                model.addAttribute("tipInfo","验证码不能为空");
                account = null;
                return "/account/Login";
            }else if(validString.compareToIgnoreCase(valistr)==0){
                //验证码正确，登录成功
                return "/catalog/Main";
            }else {
                //验证码错误，提示登陆失败。
                model.addAttribute("tipInfo","验证码错误");
                account = null;
                return "/account/Login";
            }
        } else {
            //用户不存在
            model.addAttribute("tipInfo","用户不存在");
            account = null;
            return "/account/Login";
        }
    }

    //请求访问注册页面
    @GetMapping("/registerPage")
    public String requestForRegisterPage(){
        return "/account/NewAccountForm";
    }

    //请求注册（点击注册按钮，能跳到这里来意味着用户名等信息都合法且正确）
    @PostMapping("/register")
    public String requestForRegister(Account account){

        System.out.println(account.getUsername()+account.getPassword()+account.getAddress1()+account.getEmail());
        //向数据库中插入账号信息
        return "/catalog/Main";
    }

    //查询数据库中有无叫这个名字的account
    @GetMapping("/findAccountByUsername")
    @ResponseBody
    public String findAccountByUsername(String username){
        //查询数据库中看有无叫这个名字的account
        System.out.println("我现在要找叫这个名字的account"+username);
//        Account
        account = accountService.getAccount(username);
        //如果找不到，则这个名字可以用
        if(account == null){
            return "Not Exist";
        }else {
            return "Exist";
        }
    }



    //请求查看日志
    @GetMapping("/viewMyLog")
    public String viewLogs(Model model){
        List<Log> logList = accountService.getLogsByUsername("username");
        model.addAttribute("logList",logList);
        return "/account/ViewLog";
    }


    //验证码生成
    @GetMapping("/creatValidString")
    public void getValidImage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
        Vertify vertify = new Vertify();
        vertify.drawImage(httpServletResponse.getOutputStream());
        //控制浏览器不要缓存验证码图片
        httpServletResponse.setHeader("Pragma", "no-cache");
        httpServletResponse.setHeader("Cache-Control", "no-cache");
        //将生成的验证码的文本内容输出到控制台
        validString = vertify.getCode();
        System.out.println("text=" + vertify.getCode());
    }

    //实时判断验证码
    @GetMapping("/judgeValidString")
    @ResponseBody
    public String judgeValidString(String valistr){
        System.out.println(valistr);
        //正确验证码
        String text = validString;
        System.out.println(text);
        //忽略大小写
        if(text.compareToIgnoreCase(valistr)==0){
            System.out.println("this is right");
            return "Right";
        }else{
            System.out.println("this is wrong");
            return "Wrong";
        }


    }

}

